package api;

import java.util.HashMap;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import beans.ApiRequest;
import beans.ApiResponse;
import beans.GetSmsDeliverBean;
import beans.GetSmsDeliverResBean;
import enums.ApiStatus;
import enums.CmdType;
import helper.LogHelper;
import helper.WebHelper;

public class SmsDeliverApi {
	private LogHelper logger = new LogHelper();
	private ApiRequest<GetSmsDeliverBean> request = new ApiRequest<GetSmsDeliverBean>();

	/**
	 * 查询用户上行短信
	 * @param url 提交地址
	 * @param getSmsDeliverBean 查询上行短信模型
	 * @param companyNo 公司编号
	 * @param token 令牌
	 * @param privateKey 私钥
	 * @throws Exception 
	 * @apiNote 本例为同步提交短信，同步提交只适用于点对点发送，批量提交可能导致性能降低，建议使用异步发送方法批量提交
	 * */
	public GetSmsDeliverResBean[] GetSmsDeliver(String url, GetSmsDeliverBean getSmsDeliverBean, String companyNo, String token) throws Exception {
		Gson gson = new Gson();
		request.setCmdType(CmdType.Delivery);
		request.setData(getSmsDeliverBean);
		String data = gson.toJson(request).replace(" ", "").replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "");
		System.out.println(data);
		HashMap<String, String> headers = WebHelper.GetHeaders(companyNo, token, data);
		String res = WebHelper.SendPost(url, data, headers);
		System.out.println(res);
		try {
			ApiResponse<GetSmsDeliverResBean[]> info = gson.fromJson(res, new TypeToken<ApiResponse<GetSmsDeliverResBean[]>>() {}.getType());
//			System.out.println(gson.toJson(info));
//			System.out.println("查询响应状态：" + ApiStatus.codeOf(info.getApiStatus()));
			
			if(info.getError() != "" && info.getError() != null) {
				logger.Debug(SendSmsApi.class.getName() + ".SendTemplateSms", info.getError());
				throw new Exception("getError: "+info.getError());
			}
			return info.getData();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return null;
	}
}
